package com.jd.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/9/23/023.
 */
@Component
@Aspect
public class LogHandler {
    //使用logback日志
    public static final Logger logger= LoggerFactory.getLogger(LogHandler.class);

    //切点
    @Pointcut("execution(public * com.jd.controller.*.*(..))")
    public void log(){}

    //通知（增强）
    @Before("log()")
    public void doBefor(JoinPoint pjp){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest req = attributes.getRequest();
        logger.info("uri={}",req.getRequestURI());//获取请求地址
        logger.info("ip={}",req.getRemoteHost());//获取ip地址
        logger.info("args={}",pjp.getArgs());//获取请求参数
        logger.info("method={}",pjp.getSignature().getName());//获取调用的方法名
        //TODO 后期可以将这些日志信息保存在mysql中
    }
}
